<?php
namespace Home\Controller;
use Server\PayService;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class IndexController extends BaseController {
    public function __construct() {
		parent::__construct();
        $user_id = session('user_id');
        $cart_number = M('cart')->where(array('user_id'=>$user_id))->sum('goods_number');
        $this->assign('cart_number',$cart_number);
        $this->assign('act','index');
    }
    public function index(){
        $ad_mod = M('ad');
        $store_mod = M('goods_store');
        $ad_list = $ad_mod->where(array('pid'=>1))->order('orderby desc')->select();
        $this->assign('ad_list',$ad_list);
        $goods_mod = M('goods');
        $hot_list = $goods_mod->where(array('status'=>1,'is_hot'=>1))->order('ordid desc')->select();
        foreach((array)$hot_list as $k=>$v){
            $hot_list[$k]['store_list'] = $store_mod->alias('as a')->join(C('DB_PREFIX').'business as b on a.store_id=b.id')->where(array('a.goods_id'=>$v['id']))->field('b.store_name,b.id')->select();
        }
        $this->assign('hot_list',$hot_list);
        $new_list = $goods_mod->where(array('status'=>1,'is_new'=>1))->order('ordid desc')->select();
        foreach((array)$new_list as $k=>$v){
            $new_list[$k]['store_list'] = $store_mod->alias('as a')->join(C('DB_PREFIX').'business as b on a.store_id=b.id')->where(array('a.goods_id'=>$v['id']))->field('b.store_name,b.id')->select();
        }
        $this->assign('new_list',$new_list);
        //限购列表
        $time = time();
        $purchase = M('goods_promotion')->alias('as a') ->join(C('DB_PREFIX').'goods_store_spec as b on a.spec_id=b.id')
            ->join(C('DB_PREFIX').'goods as c on a.goods_list=c.id')
            ->join(C('DB_PREFIX').'business as d on a.store_id=d.id')
            ->field('a.id,b.goods_id,a.store_id,c.goods_name,d.store_name,a.use_price as spec_price,b.spec_price as spec_mk_price,a.mall_number,a.use_number,c.thumb,a.end_time')
            ->where(array('b.status'=>1,'a.status'=>1,'a.start_time'=>array('lt',$time),'a.end_time'=>array('gt',$time),'a.type_id'=>3))->find();
        $purchase['end_time'] = date('Y/m/d H:i:s',$purchase['end_time']);
        $this->assign('purchase',$purchase);
        $this->display();
    }
    //抢购
    public function purchase(){
        $time = time();
        $purchase = M('goods_promotion')->alias('as a') ->join(C('DB_PREFIX').'goods_store_spec as b on a.spec_id=b.id')
            ->join(C('DB_PREFIX').'goods as c on a.goods_list=c.id')
            ->join(C('DB_PREFIX').'business as d on a.store_id=d.id')
            ->field('a.id,c.goods_name,a.goods_list as goods_id,a.store_id,a.spec_id,b.spec_name,d.store_name,a.use_price as spec_price,b.spec_price as spec_mk_price,a.use_number,a.mall_number,c.thumb,a.end_time')
            ->where(array('a.status'=>1,'a.start_time'=>array('lt',$time),'a.end_time'=>array('gt',$time),'a.type_id'=>3))->select();
		foreach((array)$purchase as $k=>$v){
			$purchase[$k]['use_number'] = $v['use_number']-$v['mall_number'];
		}
        $this->assign('purchase',$purchase);
        $this->display();
    }
    //搜索页
    public function search(){
        $mod = M('config');
        $hot_list = $mod->where(array('name'=>'labels'))->getField('value');
        $hot_list = str_replace('，',',',$hot_list);
        $this->assign('hot_list',explode(',',$hot_list));
        $this->assign('history',get_history());
        $this->display();
    }
    //搜索后的商品列表页
    public function goods_list(){
        $mod = M('goods_store_spec');
        $keywords = I('keywords')?I('keywords'):'';
        $type_id = I('type_id')?I('type_id'):1;
        $desc = I('desc')?I('desc'):'';
        if(I('act')=='ajax') {
            if($type_id==1){
                $order_by = 'b.id asc';
            }else if($type_id==2){
                $order_by = 'b.up_time asc';
            }else if($type_id==3){
                $order_by = 'a.sale_number desc';
            }else if($type_id==4){
                $order_by = 'a.spec_price '.$desc;
            }
            $where = array('a.status'=>1);
            $keywords = $where['b.goods_name'] = array('like','%'.$keywords.'%');
            $count = $mod->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->where($where)
                ->count();
            $page_count = 10;
            $now_page = I('page') ? I('page') : 1;
            $Page = new AjaxPage($count, $page_count);
            $total_pages = ceil($count / $page_count);
            $is_page = 0;
            if ($now_page >= $total_pages) {
                $is_page = 1;
            }
            $goods_list = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->join('left join ' . C('DB_PREFIX') . 'business as c on c.id=a.store_id')
                ->where($where)
                ->field('b.up_time,b.id as goods_id,b.goods_name,b.thumb,a.store_id,c.store_name,a.spec_price as shop_price,a.id as spec_id,a.spec_name,a.is_sold')
                ->order($order_by)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            foreach ((array)$goods_list as $k => $v) {
                $goods_list[$k]['spec_name'] = $v['spec_name'] ? '规格：' . $v['spec_name'] : '';
                $goods_list[$k]['left_price'] = intval($goods_list[$k]['shop_price']);
                $goods_list[$k]['right_price'] = substr($goods_list[$k]['shop_price'], -3, 3);
            }
            exit(json_encode(array('is_page' =>$is_page, 'result' => $goods_list)));
        }
        set_history(array('title'=>$keywords));
        $this->assign('keywords',$keywords);
        $this->assign('type_id',$type_id);
        $this->assign('desc',$desc);
        $this->display();
    }
    //商品详情
    public function detail(){
        $id = I('id')?I('id'):'';
        $store_id = I('store_id')?I('store_id'):'';
        if(!$id){
            $this->error('非法操作',U('Home/Index/index'));
        }
        $goods_mod = M('goods');
        $goods_img_mod = M('goods_img');
        $info = $goods_mod->alias('as a')->join(C('DB_PREFIX').'goods_store as b on a.id=b.goods_id')->field('a.*,b.store_id')->where(array('b.goods_id'=>$id,'b.store_id'=>$store_id,'b.status'=>1))->find();
        if(!$info['id']){
            $this->error('非法操作',U('Home/Index/index'));
        }
        $goods_img = $goods_img_mod->where(array('goods_id'=>$id))->select();
        $info['shop_price'] = '￥<span>'.intval($info['shop_price']).'</span>'.substr($info['shop_price'], -3, 3);
        $info['mk_price'] = $info['mk_price']>0?'￥<span>'.intval($info['mk_price']).'</span>'.substr($info['mk_price'], -3, 3):'';
        $info['spec'] = M('goods_store_spec')->where(array('goods_id'=>$info['id'],'store_id'=>$store_id))->select();
        foreach((array)$info['spec'] as $k=>$v){
            $info['spec'][$k]['spec_price'] = '￥<span>'.intval($v['spec_price']).'</span>'.substr($v['spec_price'], -3, 3);
            if($v['spec_mk_price']>$v['spec_price']){
                $info['spec'][$k]['spec_mk_price'] = $v['spec_mk_price'] > 0 ? '原价:' . $v['spec_mk_price'] : '';
            }else{
                $info['spec'][$k]['spec_mk_price'] = '';
            }
        }
		$info['store_name'] = M('business')->where(array('id'=>$info['store_id']))->getField('store_name');
        $this->assign('info',$info);
        $this->assign('goods_img',$goods_img);
        $comment_mod = M('orders_comment');
        $comment_list = $comment_mod->alias('as a')->join(C('DB_PREFIX').'users as b on a.user_id=b.id')->where(array('a.goods_id'=>$id))->field('a.contents,b.head_picture,a.star,a.add_time,b.user_name,a.images')->select();
        foreach((array)$comment_list as $k=>$v){
            $v['images'] && $comment_list[$k]['images'] = explode(',',$v['images']);
        }
        $this->assign('comment_list',$comment_list);
        $this->assign('comment_number',$comment_mod->where(array('goods_id'=>$id))->avg('star'));
        $c_number = $comment_mod->where(array('goods_id'=>$id))->count();
        $g_number = $comment_mod->where(array('goods_id'=>$id,'star'=>array('gt',3)))->count();
        $this->assign('good_number',round($g_number/$c_number));
        $this->display();
    }
    //添加收藏
    public function add_collect(){
        $user_id = session('user_id');
        $goods_id = I('goods_id')?I('goods_id'):'';
        $store_id = I('store_id')?I('store_id'):'';
        $mod = M('users_collect');
        if($user_id){
            if($goods_id && $store_id){
                $check = $mod->where(array('goods_id'=>$goods_id,'store_id'=>$store_id,'user_id'=>$user_id))->count();
                if($check){
                    exit(json_encode(array('code'=>100,'msg'=>'已收藏')));
                }else{
                    $mod->add(array('goods_id'=>$goods_id,'store_id'=>$store_id,'add_time'=>time(),'user_id'=>$user_id));
                    exit(json_encode(array('code'=>200,'msg'=>'收藏成功')));
                }
            }
        }else{
            exit(json_encode(array('code'=>100,'msg'=>'请先登录')));
        }
    }
    //店铺
    public function store(){
        $store_id = I('id')?I('id'):'';
        if($store_id){
            $goods_mod = M('goods_store');
            $count = $goods_mod->where(array('store_id' => $store_id, 'status' => 1))->count();
            $Page = new AjaxPage($count, 10);
            $mod = M('business');
            $info = $mod->where(array('id'=>$store_id))->find();
            $this->assign('info', $info);
            $this->assign('store_id', $store_id);
            $this->display();
        }else{
            $this->error('非法操作',U('Home/Index/index'));
        }
    }
    //店铺列表
    public function ajaxstore(){
        $mod = M('business');
        $store_id = I('store_id')?I('store_id'):'';
        if($store_id) {
            $info = $mod->where(array('id' => $store_id))->find();
            $this->assign('info', $info);
            $goods_mod = M('goods_store_spec');
			$where = array('a.store_id' => $store_id, 'a.status' => 1);
            $count = $goods_mod->alias('as a')->where($where)->count();
            $page_count = 10;
            $now_page = I('page')?I('page'):1;
            $Page = new AjaxPage($count,$page_count);
            $total_pages=ceil($count/$page_count);
            $is_page = 0;
            if($now_page>=$total_pages){
                $is_page = 1;
            }
            $goods_list = $goods_mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->join('left join ' . C('DB_PREFIX') . 'business as c on c.id=a.store_id')
                ->where($where)
                ->field('a.up_time,b.id as goods_id,b.goods_name,b.thumb,a.store_id,c.store_name,a.id as spec_id,a.spec_price as shop_price,a.spec_name,a.is_sold')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
			foreach ((array)$goods_list as $k => $v) {
                $goods_list[$k]['spec_name'] = $v['spec_name'] ? '规格：' . $v['spec_name'] : '';
                $goods_list[$k]['left_price'] = intval($goods_list[$k]['shop_price']);
                $goods_list[$k]['right_price'] = substr($goods_list[$k]['shop_price'], -3, 3);
            }
			
            exit(json_encode(array('is_page'=>$is_page,'result'=>$goods_list)));
        }
    }
    //新品
    public function new_goods(){
        $mod = M('goods_store_spec');
        $type_id = i('type')?i('type'):1;
        if(I('act')=='ajax') {
            $where = array('a.status'=>1);
            if($type_id==1){
                $where['b.is_new'] = 1;
            }else if($type_id==2){
                $where['b.is_hot'] = 1;
            }
			$goods_list = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->join('left join ' . C('DB_PREFIX') . 'business as c on c.id=a.store_id')
                ->where($where)
                ->field('b.up_time,b.id as goods_id,a.id as spec_id,b.goods_name,b.thumb,a.store_id,c.store_name,a.spec_price as shop_price,a.spec_name')
                ->order($order_by)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            foreach ((array)$goods_list as $k => $v) {
                $goods_list[$k]['spec_name'] = $v['spec_name'] ? '规格：' . $v['spec_name'] : '';
                $goods_list[$k]['left_price'] = intval($goods_list[$k]['shop_price']);
                $goods_list[$k]['right_price'] = substr($goods_list[$k]['shop_price'], -3, 3);
            }
            exit(json_encode(array('is_page' =>1, 'result' => $goods_list)));
        }
        $this->assign('type_id',$type_id);
        $this->display();
    }
	
	//清空历史搜索记录
	function clear_history(){
		cookie('history',null);
		exit(json_encode(array('code' =>200, 'result' =>'清除成功')));
	}
}